﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;
using System.Text.RegularExpressions;

namespace CungMuaC5.Account_Admin.QuanLyTaiKhoan
{
    public partial class ThemTaiKhoan : System.Web.UI.Page
    {
        string NgaySinh;
        protected void Page_Load(object sender, EventArgs e)
        {
            filldropdownlist();
            Label48.Visible = false;
            Label50.Visible = false;
            Label51.Visible = false;
            Label52.Visible = false;
            Label53.Visible = false;
            Label54.Visible = false;
            int LoaiTK = Convert.ToInt32(DropDownList2.SelectedItem.Value);
            if (LoaiTK == 2)
            {
                Label22.Visible = true;
                Label24.Visible = true;
                Label26.Visible = true;
                Label27.Visible = true;
                Label28.Visible = true;
                Label31.Visible = true;
                Label33.Visible = true;

                TextBox6.Visible = true;
                TextBox7.Visible = true;
                TextBox8.Visible = true;
                TextBox9.Visible = true;
                TextBox12.Visible = true;
                DropDownList3.Visible = true;
                DropDownList4.Visible = true;
            }
            else
            {
                Label22.Visible = false;
                Label24.Visible = false;
                Label26.Visible = false;
                Label27.Visible = false;
                Label28.Visible = false;
                Label31.Visible = false;
                Label33.Visible = false;

                TextBox6.Visible = false;
                TextBox7.Visible = false;
                TextBox8.Visible = false;
                TextBox9.Visible = false;
                TextBox12.Visible = false;
                DropDownList3.Visible = false;
                DropDownList4.Visible = false;
            }
        }

        private void filldropdownlist()
        {
            BUS_TaiKhoan bus = new BUS_TaiKhoan();
            List<DTO_KhuVuc> list = bus.KhuVuc();
            DropDownList3.DataSource = list;
            DropDownList3.DataValueField = "MaKV";
            DropDownList3.DataTextField = "TenKhuVuc";
            DropDownList3.DataBind();

            List<DTO_LoaiKM> list1 = bus.LoaiKM();
            DropDownList4.DataSource = list1;
            DropDownList4.DataValueField = "MaLoai";
            DropDownList4.DataTextField = "TenLoai";
            DropDownList4.DataBind();
        }
        public static bool isValid(string input,int type)
        {

            bool a = true;
            #region checkdigit
            if (type == 3)
            {
                char[] digit = { 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M' };
                for (int i = 0; i < digit.LongLength; i++)
                {
                    if (input.IndexOf(digit[i]) != -1)
                        return false;

                }
            }
            int check=input.IndexOf('`');
            if (check != -1)
                return false;
            check = input.IndexOf('!');
            if (check != -1)
                return false;
            check = input.IndexOf('~');
            if (check != -1)
                return false;
            if(type!=1)
            {
                check = input.IndexOf('@');
                if (check != -1)
                    return false;
            }
            check = input.IndexOf('#');
            if (check != -1)
                return false;
            check = input.IndexOf('$');
            if (check != -1)
                return false;
            if (type != 2)
            {
                check = input.IndexOf(' ');
                if (check != -1)
                    return false;
            }
            check = input.IndexOf('%');
            if (check != -1)
                return false;
            check = input.IndexOf('^');
            if (check != -1)
                return false;
            check = input.IndexOf('&');
            if (check != -1)
                return false;
            check = input.IndexOf('*');
            if (check != -1)
                return false;
            check = input.IndexOf('(');
            if (check != -1)
                return false;
            check = input.IndexOf(')');
            if (check != -1)
                return false;
            check = input.IndexOf('-');
            if (check != -1)
                return false;
            check = input.IndexOf('_');
            if (check != -1)
                return false;
            check = input.IndexOf('+');
            if (check != -1)
                return false;
            check = input.IndexOf('=');
            if (check != -1)
                return false;
            check = input.IndexOf('[');
            if (check != -1)
                return false;
            check = input.IndexOf('{');
            if (check != -1)
                return false;
            check = input.IndexOf(']');
            if (check != -1)
                return false;
            check = input.IndexOf('}');
            if (check != -1)
                return false;
            check = input.IndexOf('|');
            if (check != -1)
                return false;
            check = input.IndexOf(';');
            if (check != -1)
                return false;
            check = input.IndexOf(':');
            if (check != -1)
                return false;
            check = input.IndexOf(',');
            if (check != -1)
                return false;
            check = input.IndexOf('<');
            if (check != -1)
                return false;
            check = input.IndexOf('>');
            if (check != -1)
                return false;
            if (type != 1)
            {
                check = input.IndexOf('.');
                if (check != -1)
                    return false;
            }
            check = input.IndexOf('?');
            if (check != -1)
                return false;
            check = input.IndexOf('/');
            if (check != -1)
                return false;
            check = input.IndexOf('"');
            if (check != -1)
                return false;
            
            #endregion
            return a;
        } 
        protected void Button2_Click(object sender, EventArgs e)
        {
            
            int LoaiTK = Convert.ToInt32(DropDownList2.SelectedItem.Value);
            int ok = 1;
            BUS_TaiKhoan bus = new BUS_TaiKhoan();
            if (LoaiTK == 2)
            {
                string HoTen = TextBox13.Text;
                string Email = TextBox14.Text;
                string Password = TextBox1.Text;
                string DienThoai = TextBox5.Text;

                string ChucVu=TextBox6.Text;
                string TenDoanhNghiep = TextBox7.Text;
                string Website = TextBox8.Text;
                string DiaChi = TextBox9.Text;
                int LoaiHinhKinhDoanh = Convert.ToInt32(DropDownList4.SelectedItem.Value);
                int NoiMuonQuangCao = Convert.ToInt32(DropDownList3.SelectedItem.Value);
                string ThongTinHopTac=TextBox12.Text;
                int GioiTinh = Convert.ToInt32(DropDownList1.SelectedItem.Value);
                string confirm = TextBox15.Text;

                if (confirm != Password)
                {
                    Label50.Visible = true;
                    ok = 0;
                }
                if (DiaChi == "")
                {
                    Label46.Visible = true;
                    ok = 0;
                    
                }
                else
                {
                    Label46.Visible = false;
                }
                if (isValid(DiaChi, 2) == false)
                {
                    Label51.Visible = true;
                    ok = 0;
                    goto Label;
                }
                else
                {
                    Label51.Visible = false;
                }
                if (TenDoanhNghiep == "")
                {
                    Label43.Visible = true;
                    ok = 0;
                }
                else
                {
                    Label43.Visible = false;
                }
                if (isValid(TenDoanhNghiep, 2) == false)
                {
                    Label51.Visible = true;
                    ok = 0;
                }
                else
                {
                    Label51.Visible = false;
                }
                if (ChucVu == "")
                {
                    Label45.Visible = true;
                    ok = 0;
                }
                else
                {
                    Label45.Visible = false;
                }
                if (isValid(ChucVu, 2) == false)
                {
                    Label51.Visible = true;
                    ok = 0;
                    goto Label;
                }
                else
                {
                    Label51.Visible = false;
                }
                if (HoTen == "")
                {
                    Label40.Visible = true;
                    ok = 0;
                }
                else
                {
                    Label40.Visible = false;
                }
                if (isValid(HoTen, 2) == false)
                {
                    Label51.Visible = true;
                    ok = 0;
                    goto Label;
                }
                else
                {
                    Label51.Visible = false;
                }
                if (Email == "")
                {
                    Label41.Visible = true;
                    ok = 0;
                }
                else
                {
                    Label41.Visible = false;
                }
                if (isValid(Email, 1) == false)
                {
                    Label51.Visible = true;
                    ok = 0;
                    goto Label;
                }
                else
                {
                    Label51.Visible = false;
                }
                if (Password == "")
                {
                    Label38.Visible = true;
                    ok = 0;
                }
                else
                {
                    Label38.Visible = false;
                }
                if (isValid(Password, 0) == false)
                {
                    Label51.Visible = true;
                    ok = 0;
                    goto Label;
                }
                else
                {
                    Label51.Visible = false;
                    if (Password.Length < 6 || Password.Length > 14)
                    {
                        Label53.Visible = true;
                        ok = 0;
                    }
                }
                if (DienThoai == "")
                {
                    Label42.Visible = true;
                    ok = 0;
                }
                else
                {
                    Label42.Visible = false;
                }
                if (isValid(DienThoai, 3) == false)
                {
                    Label52.Visible = true;
                    ok = 0;
                    goto Label;
                }
                else
                {
                    Label51.Visible = false;
                }
            Label: int nothing;
                bool KT=KTNgaySinh();
                if (KT == false)
                {
                    ok = 0;
                    Label54.Visible = true;
                }
                if (ok == 1)
                {
                    bool j = bus.KTEmail(Email);
                    bool i;
                    if (j == true)
                    {
                        i = bus.Add(HoTen, Email, Password, DienThoai, LoaiTK, GioiTinh, NgaySinh);
                        int a = bus.TimMaTK(Email);
                        i = bus.Addbosung(ChucVu,TenDoanhNghiep,Website,DiaChi,LoaiHinhKinhDoanh,NoiMuonQuangCao,ThongTinHopTac,a);
                        if(i==false)
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript'>alert('Thêm Người Dùng Thất Bại');</script>");
                        else
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript'>alert('Thêm Người Dùng Thành Công');</script>");

                    }
                    else
                        Label48.Visible = true;
                }
            }
            else
            {
                string HoTen=TextBox13.Text;
                string Email=TextBox14.Text; 
                string Password=TextBox1.Text;
                string DienThoai = TextBox5.Text;
                int GioiTinh = Convert.ToInt32(DropDownList1.SelectedItem.Value);
                string confirm = TextBox15.Text;

                if (confirm != Password)
                {
                    Label50.Visible = true;
                    ok = 0;
                }
                if (HoTen == "")
                {
                    Label40.Visible = true;
                    ok = 0;
                }
                else
                {
                    Label40.Visible = false;
                }
                if (isValid(HoTen, 2) == false)
                {
                    Label51.Visible = true;
                    ok = 0;
                    goto Label;
                }
                else
                {
                    Label51.Visible = false;
                }
                if (Email == "")
                {
                    Label41.Visible = true;
                    ok = 0;
                }
                else
                {
                    Label41.Visible = false;
                }
                if (isValid(Email, 1) == false)
                {
                    Label51.Visible = true;
                    ok = 0;
                    goto Label;
                }
                else
                {
                    Label51.Visible = false;
                }
                if (Password == "")
                {
                    Label38.Visible = true;
                    ok = 0;
                }
                else
                {
                    Label38.Visible = false;
                }
                if (isValid(Password, 0) == false)
                {
                    Label51.Visible = true;
                    ok = 0;
                    goto Label;
                }
                else
                {
                    Label51.Visible = false;
                }
                if (DienThoai == "")
                {
                    Label42.Visible = true;
                    ok = 0;
                }
                else
                {
                    Label42.Visible = false;
                }
                if (isValid(DienThoai, 3)==false)
                {
                    Label52.Visible = true;
                    ok = 0;
                }
                else
                {
                    Label52.Visible = false;
                    if (Password.Length < 6 || Password.Length > 14)
                    {
                        Label53.Visible = true;
                        ok = 0;
                    }
                }
            Label: int nothing;
                bool KT = KTNgaySinh();
                if (KT == false)
                {
                    ok = 0;
                    Label54.Visible = true;
                }
                if (ok == 1)
                {
                    bool j = bus.KTEmail(Email);
                    bool i=true;
                    if(j==true)
                        i = bus.Add(HoTen, Email, Password, DienThoai, LoaiTK, GioiTinh, NgaySinh);
                    else
                        Label48.Visible=true;
                    if (i == false)
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript'>alert('Thêm Người Dùng Thất Bại');</script>");
                    else
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript'>alert('Thêm Người Dùng Thành Công');</script>");
                }
            }
        }

        private bool KTNgaySinh()
        {
            bool a = true;
            int Ngay = Convert.ToInt16(DropDownList5.SelectedItem.Value);
            int Thang = Convert.ToInt16(DropDownList6.SelectedItem.Value);
            int Nam;
            try
            {
                Nam = Convert.ToInt32(TextBox16.Text);
            }
            catch(Exception ex)
            {
                return false;
            }
            if(Thang==1 ||Thang==3 ||Thang==5 ||Thang==7 || Thang==8 || Thang==10 || Thang==12 )
            {
            }
            if (Thang == 4 || Thang == 6 || Thang == 9 || Thang == 11)
            {
                if (Ngay == 31)
                    return false;
            }
            if (Thang == 2)
            {
                if (Nam % 4 == 0)
                {
                    if (Ngay >= 30)
                        return false;
                }
                else
                {
                    if (Ngay >= 29)
                        return false;
                }
            }
            int Year=Convert.ToInt32(GetYear(1));
            if (Nam >= Year)
                return false;
            NgaySinh = Thang + "/" +Ngay +"/" + Nam;
            return a;
        }
        public string GetYear(int i)
        {
            string str = DateTime.Now.ToString().Trim();
            string str1 = str.Substring(0, 2);
            try
            {
                int a = Convert.ToInt32(str1);
                str = str.Substring(6, 3);

            }
            catch (Exception ee)
            {
                str = str.Substring(5, 3);

            }
            str = "2" + str;
            return str;
        }
        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
    }
}